package com.app.business.web.business;

import com.app.business.en.HouseStatus;
import com.app.business.model.*;
import com.app.business.service.AzProjectService;
import com.app.business.service.HouseService;
import com.app.business.service.ZsProjectHouseService;
import com.app.business.service.ZsProjectService;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 房源分配
 * Created by yby on 2019/2/14 16:33.
 */

@Controller
@RequestMapping("/business/zsProjectHouse/*")
@SuppressWarnings("all")
public class ZsProjectHouseController {

    @Resource
    private ZsProjectService zsProjectService;
    @Resource
    private HouseService houseService;
    @Resource
    private DataDictService dataDictService;
    @Resource
    private ZsProjectHouseService zsProjectHouseService;
    @Resource
    private AzProjectService azProjectService;

    /**
     * 征收项目数据列表
     */
    @RequestMapping("zsProjectList")
    @RequiresPermissions("zsProjectHouse:zsProjectList")
    public void list() {
    }

    /**
     * 加载征收项目数据
     *
     * @return
     */
    @RequestMapping("loadZsProjectList")
    @ResponseBody
    @RequiresPermissions("zsProjectHouse:loadZsProjectList")
    public Map loadFamilyList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<ZsProject> zsProjectPageBean = this.zsProjectService.getZsProjectPageBean();
        List<ZsProject> zsProjectList = zsProjectPageBean.getDatas();
        Map<String,String> townMap = dataDictService.getMapByParentNameForValue_Name("乡镇");
        if (!CollectionUtils.isEmpty(zsProjectList)) {
            Map<String, Object> item = null;
            for (ZsProject zsProject : zsProjectList) {
                item = Maps.newHashMap();
                item.put("id", zsProject.getId());
                item.put("town", StringUtils.getNotNull(townMap.get(zsProject.getTown())));
                item.put("projectCode", StringUtils.getNotNull(zsProject.getProjectCode()));
                item.put("projectName", StringUtils.getNotNull(zsProject.getProjectName()));
                item.put("azProjectName", StringUtils.getNotNull(zsProject.getAzProjectName()));
                item.put("memo", StringUtils.getNotNull(zsProject.getMemo()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(zsProjectPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 房源分配
     * @param request
     * @param modelMap
     */
    @RequestMapping("houseTree")
    @RequiresPermissions("zsProjectHouse:houseTree")
    public void housePicture(HttpServletRequest request, ModelMap modelMap) {
        Integer zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "分配的征收项目不存在");
        modelMap.addAttribute("zsProject", zsProject);

        List<DataDict> dataDictList = dataDictService.findListByParentName("房源状态");
        modelMap.addAttribute("dataDictList", dataDictList);
    }

    /**
     * 获取树形菜单
     * @return
     */
    @RequestMapping("loadTreeData")
    @ResponseBody
    @RequiresPermissions("zsProjectHouse:loadTreeData")
    public Map loadTreeData() {

        List<Map> childrenList = this.houseService.mapTreeData();
        Map map = Maps.newLinkedHashMap();
        map.put("id", null);
        map.put("name", "安置项目");
        map.put("spread", true);
        map.put("children", childrenList);

        return map;
    }

    /**
     * 加载楼盘图
     * @param request
     * @return
     */
    @RequestMapping("loadHousePicture")
    @ResponseBody
    @RequiresPermissions("zsProjectHouse:loadHousePicture")
    public Map loadHousePicture(HttpServletRequest request) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        String buildNum = ParamUtils.getString(request, "buildNum", "");
        Integer zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);

        //查出所有房源
        List<House> houseList = houseService.houseList(azProjectId, buildNum);

        //得到层号和当前楼层的List<House>对应的map
        Map<String, List<House>> floorHouseMap = houseService.getFloorHouseMap(houseList);

        //房源状态
        Map<String, String> colorMap = this.dataDictService.getMapByParentNameForValue_Color("房源状态");
       //房屋类型
        Map<String, String> houseHoldMap = this.dataDictService.getMapByParentNameForValue_Name("房屋户型");
        //得到征收项目某栋楼中已经分配的房源
        Map<String, String> houseIdMap = zsProjectHouseService.getHouseIdMap(zsProjectId, azProjectId, buildNum);

        Map<String,Integer> maxFloorMap = houseService.getMaxFloorMap(azProjectId, buildNum);
        Integer maxFloor = maxFloorMap.get("maxFloor");

        AzProject azProject = azProjectService.findOne(azProjectId);

        Map<String, Object> map = Maps.newHashMap();
        map.put("maxFloor", maxFloor);
        map.put("floorHouseMap", floorHouseMap);
        map.put("colorMap", colorMap);
        map.put("houseHoldMap", houseHoldMap);
        map.put("houseIdMap", houseIdMap);
        map.put("azProject", azProject);
        return map;
    }

    /**
     * 保存房源权限
     *
     * @param request
     * @return
     */
    @RequestMapping("saveZsProjectHouse")
    @ResponseBody
    @RequiresPermissions("zsProjectHouse:saveZsProjectHouse")
    public synchronized Response saveZsProjectHouse(HttpServletRequest request) {
        zsProjectHouseService.saveZsProjectHouse(request);
        return new Response("0001","保存成功");
    }

    /**
     * 加载房源分配信息
     * @param request
     * @param modelMap
     */
    @RequestMapping("loadZsProjectHouseInfo")
    @RequiresPermissions("zsProjectHouse:loadZsProjectHouseInfo")
    public synchronized void loadZsProjectHouseInfo(HttpServletRequest request,ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request,"azProjectId",0);
        String buildNum = ParamUtils.getString(request,"buildNum","");
        List<Integer> houseIdList = zsProjectHouseService.getHouseIdList(azProjectId,buildNum);
        Map<String,Integer> zsProjectHouseCountMap = zsProjectHouseService.getZsProjectHouseCountMap(houseIdList);
        List<ZsProject> zsProjectList = zsProjectService.zsProjectList();

        modelMap.addAttribute("zsProjectList",zsProjectList);
        modelMap.addAttribute("zsProjectHouseCountMap",zsProjectHouseCountMap);
    }

    /**
     * 房源分配批量导入
     * @param request
     * @param modelMap
     */
    @RequestMapping("batchForm")
    @RequiresPermissions("zsProjectHouse:batchForm")
    public void batchForm(HttpServletRequest request, ModelMap modelMap) {
        Integer zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "征收项目不存在!");
        modelMap.addAttribute("zsProject", zsProject);
    }

    /**
     * 房源分配批量导入保存
     * @param request
     * @param modelMap
     */
    @RequestMapping("batchFormSave")
    @ResponseBody
    @RequiresPermissions("zsProjectHouse:batchFormSave")
    public Response batchFormSave(HttpServletRequest request) {
        Response response = new Response();
        response = this.zsProjectHouseService.batchFormSave(request);
        return response;
    }
}
